<h4><?php echo $this->navigation()->breadcrumbs($this->navigation); ?></h4>

<h1>
<?php 
$crumbs = $this->navigation()->breadcrumbs($this->navigation);
echo $crumbs->setPartial(array('title.tpl', 'admin'));
?><span class="nav-item"><?php 
$menu = $this->navigation()->menu($this->navigation);
$menu->setOnlyActiveBranch(true);
$menu->setRenderParents(false);
echo strip_tags($menu, '<a>');
?>
</span>
</h1>

<div id='crud_list'>
	<?php 
	$orderDesc = isset($this->request['orderDesc']) ? $this->request['orderDesc'] : null;
	$orderBy = isset($this->request['orderBy']) ? $this->request['orderBy'] : null;
	
	echo $this->form(null, array(
		'id' => 'crud_list_search',
		'action' => $this->url(array('action' => 'ajax-list')),
	));
	echo $this->formHidden('orderDesc', $orderDesc);
	echo $this->formHidden('orderBy', $orderBy);
	
	// Filters for many2many talbes
	echo '<p id="m2mfilters_title">Дополнительная фильтрация: </p>';
	echo '<table id="m2mfilters">';
	$table = new Crud_Db_Table($this->request['table']);
	foreach ($table->getMany2ManyTables() as $m2m) {
		$column3rd = $m2m->getFKColumnNotFor($table);
		if (!$column3rd->isFK()) {
			continue;
		}
		
		$element = Crud_Form_Element::factory($column3rd)->getElement();
		
		// Set options for select
		$options = $element->getMultiOptions();
		if (isset($options[''])) unset($options['']);
		if (!$options) {
			continue;
		}
		
		$element->clearMultiOptions();
		$element->addMultiOption(0, 'Не фильтровать');
		$element->addMultiOptions($options);
		
		
		$element->setAttrib('class', 'search');
		$element->setAttrib('default', 0);
		$element->setBelongsTo('m2msearch');
		
		$element->setDecorators(array(
			'ViewHelper',
		    array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class'  => 'm2mfilters_container')),
        	array('Label', array('tag' => 'td', 'class' => 'm2mfilters_label')),
        	array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
        ));
        
        // Set label for search column
		$table3rd = $column3rd->getFKTable();
		if ($table3rd->getConfig() && $table3rd->getConfig()->label) {
			$element->setLabel($table3rd->getConfig()->label);
		}
		
		$element->setName($m2m->getTableName() . '__' . $element->getName());
		echo $element;
	}
	echo '</table>';

	if (count($this->rowset)) { ?>
	<table id='crud_list'>
		<thead>
		<tr>
			<?php foreach ($this->columns as $name => $column) {
				$orderUrlParams = array('orderBy' => $name, 'page' => null);
				if ($orderDesc) {
					$orderUrlParams['orderDesc'] = null;
					$title = 'Сортировать по возрастанию';
				} else {
					$orderUrlParams['orderDesc'] = 1;
					$title = 'Сортировать по убыванию';
				}
				
				$orderUrl = $this->url($orderUrlParams);
				$label = $column->getLabel();
				
				$class = '';
				if ($name == $orderBy) {
					if ($orderDesc) {
						$class = 'class="sortedDesc"';
					} else {
						$class = 'class="sortedAsc"';
					}
				}
				echo "<th $class><a title='$title' href='$orderUrl'>$label</a></th>";
			}?>
			<th>&nbsp;</th>
		</tr>
		<tr>
			<?php foreach ($this->columns as $name => $column) {
				$input = Crud_Form_Element::factory($column)->getElement();
				if ($input instanceof Zend_Form_Element_Multi) {
					$options = $input->getMultiOptions();
					$input->clearMultiOptions();
					$input->addMultiOption(null);
					$input->addMultiOptions($options);
				}
				
				$input->setAttrib('class', 'search');
				$input->setAttrib('default', '');
				$input->setBelongsTo('search');
				$input->setDecorators(array('ViewHelper'));
				$input->setValue(null);
				echo "<th>$input</th>";
			}?>
			<th>&nbsp;</th>
		</tr>
		</thead>
		<tbody id='crud_list_content'>
		<?php echo $this->render('list_items.tpl'); ?>
		</tbody>
	</table>

	<div id='paginator' href='<?php echo $this->url(array('action' => 'ajax-paginator'));?>'>
		<?php 
		echo $this->render('paginator.tpl');
		?>
	</div>
	
	<?php 
	} else {
		echo "<p class='table_empry'>В таблице нет ни одной записи.</p>";
	}?>
</form>
</div>
